package com.xiaotu.server.controller;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiaotu.common.model.SpiderLogModel;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.SpiderSystemService;
import com.xiaotu.spider.SupplementRunner;

@RequestMapping(value="spiderSystem")
@RestController
public class SpiderSystemController {

	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(SpiderSystemController.class);
	
	@Autowired
	private SpiderSystemService spiderSystemService;
	
	/**
	 * 记录爬虫状态接口
	 * 返回结果为{'status':'0','message':'保存成功'}
	 * @param spiderLogInfo spiderLogJson 
	 */
	@RequestMapping(value="saveSpiderLog")
	public String saveSpiderLog(@RequestBody String spiderLogInfo){
		
		if(StringUtil.isEmpty(spiderLogInfo)){
			return "{'status':'1','message':'参数不可为空'}";
		}
		
		try {
			SpiderLogModel spiderLogModel = (SpiderLogModel) JSONObject.toBean(JSONObject.fromObject(spiderLogInfo), SpiderLogModel.class);
			spiderLogModel.setTimestamp(System.currentTimeMillis());
			spiderSystemService.saveSpiderSystemLog(spiderLogModel);
		} catch (Exception e) {
			LOGGER.error("记录爬虫日志异常("+spiderLogInfo+") ",e);
			return "{'status':'1','message':'"+e.getMessage()+"'}";
		}
		
		return "{'status':'0','message':'保存成功'}";
	}
	
	
}
